package immt.ui.parameters;

import immt.algorithms.WeightedMeanFilter;
import immt.ui.ShellWindow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WeightedMeanFilterParams extends javax.swing.JPanel {

    private final ShellWindow parent;
    private WeightedMeanFilter algorithm;
    private final Logger logger = LoggerFactory.getLogger(WeightedMeanFilterParams.class);
    
    
        /**
     * *
     * Parameters for the Weighted Mean Filter
     *
     * @param parent parent of the window
     * @param algorithm weighted mean filter algorithm to apply
     */
    public WeightedMeanFilterParams(ShellWindow parent, WeightedMeanFilter algorithm) {
        initComponents();
        this.parent = parent;
        this.algorithm = algorithm;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tf_00 = new javax.swing.JTextField();
        b_Execute = new javax.swing.JButton();
        tf_01 = new javax.swing.JTextField();
        tf_02 = new javax.swing.JTextField();
        tf_10 = new javax.swing.JTextField();
        tf_11 = new javax.swing.JTextField();
        tf_12 = new javax.swing.JTextField();
        tf_20 = new javax.swing.JTextField();
        tf_21 = new javax.swing.JTextField();
        tf_22 = new javax.swing.JTextField();
        rb_option1 = new javax.swing.JRadioButton();
        rb_option2 = new javax.swing.JRadioButton();

        b_Execute.setText("Execute");
        b_Execute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_ExecuteActionPerformed(evt);
            }
        });

        rb_option1.setText("Option 1");
        rb_option1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_option1ActionPerformed(evt);
            }
        });

        rb_option2.setText("Option 2");
        rb_option2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_option2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(b_Execute, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tf_10, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tf_11, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tf_12, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tf_00, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tf_01, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tf_02, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tf_20, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tf_21, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tf_22, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(rb_option1)
                            .addComponent(rb_option2))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rb_option1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rb_option2)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_00, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_01, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_02, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_21, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_22, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(b_Execute)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    
     /**
     * *
     * Execute the algorithm selected
     *
     * @param evt
     */
    private void b_ExecuteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_ExecuteActionPerformed
        String radioInput = tf_00.getText();
        try {
            int[] kernel = loadKernel();
            algorithm.setKernel(kernel);
            
            // If it was processed before, create a new SwingWorker
            if (algorithm.getResultingImage() != null) {
                algorithm = (WeightedMeanFilter) algorithm.clone();
            }

            algorithm.setOriginalImage(parent.getOriginalImage());

            // The ShellWindow listens for updates of progress
            algorithm.addPropertyChangeListener(parent);

            algorithm.execute();
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            parent.setStatus(radioInput + " cannot be used as a radio!");
        }
    }//GEN-LAST:event_b_ExecuteActionPerformed

    public int[] loadKernel(){
        int[] kernel = new int[9];
        kernel[0] = Integer.parseInt(tf_00.getText());
        kernel[1] = Integer.parseInt(tf_01.getText());
        kernel[2] = Integer.parseInt(tf_02.getText());
        kernel[3] = Integer.parseInt(tf_10.getText());
        kernel[4] = Integer.parseInt(tf_11.getText());
        kernel[5] = Integer.parseInt(tf_12.getText());
        kernel[6] = Integer.parseInt(tf_20.getText());
        kernel[7] = Integer.parseInt(tf_21.getText());
        kernel[8] = Integer.parseInt(tf_22.getText());
        return kernel;
    }
    
    
    private void rb_option2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_option2ActionPerformed
        rb_option1.setSelected(false);
        tf_00.setText("1");
        tf_01.setText("2");
        tf_02.setText("1");
        tf_10.setText("2");
        tf_11.setText("4");
        tf_12.setText("2");
        tf_20.setText("1");
        tf_21.setText("2");
        tf_22.setText("1");
    }//GEN-LAST:event_rb_option2ActionPerformed

    private void rb_option1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_option1ActionPerformed
        rb_option2.setSelected(false);
        tf_00.setText("1");
        tf_01.setText("1");
        tf_02.setText("1");
        tf_10.setText("1");
        tf_11.setText("2");
        tf_12.setText("1");
        tf_20.setText("1");
        tf_21.setText("1");
        tf_22.setText("1");
    }//GEN-LAST:event_rb_option1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_Execute;
    private javax.swing.JRadioButton rb_option1;
    private javax.swing.JRadioButton rb_option2;
    private javax.swing.JTextField tf_00;
    private javax.swing.JTextField tf_01;
    private javax.swing.JTextField tf_02;
    private javax.swing.JTextField tf_10;
    private javax.swing.JTextField tf_11;
    private javax.swing.JTextField tf_12;
    private javax.swing.JTextField tf_20;
    private javax.swing.JTextField tf_21;
    private javax.swing.JTextField tf_22;
    // End of variables declaration//GEN-END:variables
}
